using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using ShareTools.InfoPathFormsLib;
using ShareTools.Common;

namespace ShareTools.WorkflowManager
{
    public partial class ContentTypeBasedWorkflowManager : Form
    {
        List<FormContentType> activeFCTList = new List<FormContentType>();
        Hashtable hashTable = new Hashtable();
        List<WorkflowItem> workflowsAvailable;

        public ContentTypeBasedWorkflowManager()
        {
            InitializeComponent();
        }

        private void btnSelectSite_Click(object sender, EventArgs e)
        {
            LoadScreen(0, 0);
        }

        private void LoadScreen(int ctGrpSelectedIndex, int ctSelectedIndex)
        {
            this.Refresh();
            FormsContentTypeManager fCTMgr = new FormsContentTypeManager();
            fCTMgr.SiteURL = txtSiteURL.Text.Trim();
            hashTable = fCTMgr.GetFormContentTypesList();
            workflowsAvailable = fCTMgr.GetWorkflowsAvailable();
            cbBoxCTGroups.Items.Add("All");
            PopulateWorkflowTemplates();
            foreach (string grpName in hashTable.Keys)
            {
                cbBoxCTGroups.Items.Add(grpName);
                PopulateContentTypesList(hashTable[grpName] as List<FormContentType>);
            }
            cbBoxCTGroups.SelectedIndex = 1;
            cbBoxCTGroups.SelectedIndex = ctGrpSelectedIndex;
            listBoxContentTypes.SelectedIndex = 0;
            listBoxContentTypes.SelectedIndex = ctGrpSelectedIndex;
        }

        private void PopulateContentTypesList(List<FormContentType> formCTList)
        {
            foreach (FormContentType fCT in formCTList)
            {
                listBoxContentTypes.Items.Add(fCT.Name + " (" + fCT.WorkflowAssociationsCount + ")");
                activeFCTList.Add(fCT);
            }
        }

        private void cbBoxCTGroups_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBoxContentTypes.Items.Clear();
            activeFCTList.Clear();
            if (string.IsNullOrEmpty(cbBoxCTGroups.SelectedItem as string)) return;
            if (cbBoxCTGroups.SelectedItem.Equals("All"))
            {
                for (int i = 1; i < cbBoxCTGroups.Items.Count; i++)
                {
                    PopulateContentTypesList(hashTable[cbBoxCTGroups.Items[i]] as List<FormContentType>);
                }
            }
            else
            {
                PopulateContentTypesList(hashTable[cbBoxCTGroups.SelectedItem as string] as List<FormContentType>);
            }
        }

        private void listBoxContentTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbWFAssociations.Items.Clear();

            FormContentType fCT = activeFCTList[listBoxContentTypes.SelectedIndex];
            lblActiveFormName.Text = fCT.Name;
            lblCTIDValue.Text = fCT.ContentTypeID;
            if (fCT.WorkflowAssociationsCount > 0)
            {
                foreach (string name in fCT.WorkflowNames)
                {
                    cbWFAssociations.Items.Add(name);
                }
            }
            else
            {
                cbWFAssociations.Items.Add("None");
                //cbWFAssociations.Enabled = false;
            }
        }


        private void PopulateWorkflowTemplates()
        {
            cbWFAvailable.Items.Clear();
            foreach (WorkflowItem item in workflowsAvailable)
            {
                cbWFAvailable.Items.Add(item.Name);
            }
        }

        private void btnAddRemoveWF_Click(object sender, EventArgs e)
        {
            if (cbWFAvailable.SelectedIndex < 0)
            {
                MessageBox.Show("Please select the workflow from the available workflows drop down list"); return;
            }
            if (string.IsNullOrEmpty(txtWFTaskListName.Text.Trim()) ||
                string.IsNullOrEmpty(txtWFHistoryListName.Text.Trim()) ||
                string.IsNullOrEmpty(txtWFName.Text.Trim()))
            {
                MessageBox.Show("Please provide the values for the Task List, History List and Workflow Name text boxes."); return;
            }
            if (!rdbSingleCT.Checked && !rdbAllCT.Checked)
            {
                MessageBox.Show("Please select one of the options.");
                return;
            }
            FormsContentTypeManager fctMgr = new FormsContentTypeManager();
            fctMgr.SiteURL = txtSiteURL.Text;
            if(rdbSingleCT.Checked)
                fctMgr.AddWorkflowAssociation(activeFCTList[listBoxContentTypes.SelectedIndex], cbWFAvailable.SelectedItem.ToString(),
                    txtWFTaskListName.Text, txtWFHistoryListName.Text, txtWFName.Text);
            else
                fctMgr.AddWorkflowAssociation(activeFCTList, cbWFAvailable.SelectedItem.ToString(),
                    txtWFTaskListName.Text, txtWFHistoryListName.Text, txtWFName.Text);

            LoadScreen(cbBoxCTGroups.SelectedIndex, listBoxContentTypes.SelectedIndex);
        }

        private void btnRemoveWF_Click(object sender, EventArgs e)
        {
            if (cbWFAssociations.SelectedIndex < 0)
            {
                MessageBox.Show("Please select the workflow from the drop down list"); return;
            }
            FormsContentTypeManager fctMgr = new FormsContentTypeManager();
            fctMgr.SiteURL = txtSiteURL.Text;
            fctMgr.RemoveWorkflowAssociation(activeFCTList[listBoxContentTypes.SelectedIndex], cbWFAssociations.SelectedItem.ToString().Substring(0, cbWFAssociations.SelectedItem.ToString().IndexOf("(")));
            LoadScreen(cbBoxCTGroups.SelectedIndex, listBoxContentTypes.SelectedIndex);
        }

        private void btnRemoveForAll_Click(object sender, EventArgs e)
        {
            FormsContentTypeManager fctMgr = new FormsContentTypeManager();
            fctMgr.SiteURL = txtSiteURL.Text;
            fctMgr.RemoveWorkflowAssociation(activeFCTList, txtWFNameForAll.Text);
            LoadScreen(cbBoxCTGroups.SelectedIndex, listBoxContentTypes.SelectedIndex);
        }
    }
}